package main

import (
	"context"
	"fmt"
	"github.com/go-redis/redis/v8"
	"log"
	"time"
)

type userCredentialLoginCount struct {
	ID                string // 用户id也可能是用户名
	account_pwd_count int    // 账号或密码错误次数
	token_count       int    // 令牌错误次数
}

func main() {
	fmt.Println("hello world")
	rdb := redis.NewFailoverClient(
		&redis.FailoverOptions{
			MasterName:    "master",
			SentinelAddrs: []string{"192.168.0.137:26379", "192.168.0.137:26379", "192.168.0.137:26379"},
			Password:      "",
			DB:            2,
		})

	ctx := context.Background()
	fmt.Println("client", rdb)

	/**
	连接Redis哨兵模式
	*/
	err := rdb.Ping(ctx).Err()
	if err != nil {
		fmt.Println("Ping:", err)
	}

	bExists, err := rdb.HExists(ctx, "hash_tests", "tel").Result()
	log.Println(bExists, err)
	BRet, err := rdb.HSet(ctx, "hash_tests", "tel", 30*time.Second).Result()
	log.Println(BRet, err)
	rdb.HSet(ctx, "hash_tests", "token_count", 2).Result()
	rdb.HSet(ctx, "hash_tests", "account_pwd_count", 3).Result()

}

/**
连接Redis哨兵模式
*/
func initClient() (err error) {
	rdb1 := redis.NewFailoverClient(&redis.FailoverOptions{
		MasterName:    "master",
		SentinelAddrs: []string{"192.168.0.137:26379", "192.168.0.137:26379", "192.168.0.137:26379"},
		Password:      "",
		DB:            0,
	})
	ctx := context.Background()
	_, err = rdb1.Ping(ctx).Result()
	if err != nil {
		return err
	}
	return nil
}

/**
连接Redis集群
*/
func initClientMaster() (err error) {
	rdb := redis.NewClusterClient(&redis.ClusterOptions{
		Addrs: []string{":7000", ":7001", ":7002", ":7003", ":7004", ":7005"},
	})
	ctx := context.Background()
	_, err = rdb.Ping(ctx).Result()
	if err != nil {
		return err
	}
	return nil
}

/**
ZSET
*/
func redisExample2() {
	//初始化连接池
	rdb := redis.NewClient(&redis.Options{
		Addr:     "127.0.0.1:6379",
		Password: "",
		DB:       0,
		PoolSize: 1000,
	})
	ctx := context.Background()

	zsetKey := "language_rank"
	languages := []*redis.Z{
		&redis.Z{Score: 90.0, Member: "Golang"},
		&redis.Z{Score: 98.0, Member: "Java"},
		&redis.Z{Score: 95.0, Member: "Python"},
		&redis.Z{Score: 97.0, Member: "JavaScript"},
		&redis.Z{Score: 99.0, Member: "C/C++"},
	}
	num, err := rdb.ZAdd(ctx, zsetKey, languages...).Result()
	if err != nil {
		fmt.Printf("zadd failed, err:%v\n", err)
		return
	}
	log.Println(num)
	//把Golang的分数加10
	newScore, err := rdb.ZIncrBy(ctx, zsetKey, 10, "Golang").Result()
	if err != nil {
		fmt.Printf("zincrby failed, err:%v\n", err)
		return
	}
	fmt.Printf("Golang's score is %f now.\n", newScore)
	// 取分数最高的3个
	ret, err := rdb.ZRevRangeWithScores(ctx, zsetKey, 0, 2).Result()
	if err != nil {
		fmt.Printf("zrevrange failed, err:%v\n", err)
		return
	}
	for _, z := range ret {
		fmt.Println(z.Member, z.Score)
	}
	// 取95~100分的
	op := redis.ZRangeBy{
		Min: "95",
		Max: "100",
	}
	ret, err = rdb.ZRangeByScoreWithScores(ctx, zsetKey, &op).Result()
	if err != nil {
		fmt.Printf("zrangebyscore failed, err:%v\n", err)
		return
	}
	for _, z := range ret {
		fmt.Println(z.Member, z.Score)
	}
	//按通配符删除key
	iter := rdb.Scan(ctx, 0, "prefix*", 0).Iterator()
	for iter.Next(ctx) {
		err := rdb.Del(ctx, iter.Val()).Err()
		if err != nil {
			panic(err)
		}
	}
	if err := iter.Err(); err != nil {
		panic(err)
	}
	//Pipeline
}
